package com.kapcb.mvc.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * <a>Title: OncePerApplicationStartedEventListener </a>
 * <a>Author: kapcb <a>
 * <a>Description: 某个方法仅在启动时执行一次 <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/4/24 12:47
 * @since 1.0
 */
@Slf4j
public class OncePerApplicationStartedEventListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 保证只执行一次
        if (event.getApplicationContext().getParent() == null) {
            log.info("只执行一次的方法: 例如数据预热、初始化缓存、特定任务注册等...");
        }
    }

}
